package com.bringway.p2p.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.collections.MapUtils;
import org.omg.CORBA.PUBLIC_MEMBER;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.w3c.dom.ls.LSInput;

import com.bringway.global.domain.SysUserDetails;
import com.bringway.p2p.dao.P2PUserDao;
import com.bringway.global.domain.User;
import com.bringway.p2p.service.P2PUserService;

import net.sf.json.JSON;
import net.sf.json.JSONObject;
/*
 * @author qjy
 * @className MyUserServiceImpl
 * @description 用户操作业务逻辑实现类
 * @date 2015年12月17日下午4:04:23
 */
@Service
@Transactional
public class P2PUserServiceImpl implements P2PUserService {
	
	@Autowired
	private P2PUserDao userDao;
	/*
	 * (non-Javadoc)
	 * @see com.bringway.p2p.service.MyUserService#users()
	 */
	@Override
	public List users(JSONObject parms) {
		return userDao.getAllusers(parms);
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.bringway.p2p.service.P2PUserService#usersAllLimit(net.sf.json.JSONObject)
	 */
	@Override
	public List usersAllLimit(JSONObject parms) {
		return userDao.getAllusersLimit(parms);
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.bringway.p2p.service.P2PUserService#addUser(net.sf.json.JSONObject)
	 */
	@Override
	public boolean addUser(JSONObject parms) {
		boolean bool = false;
		//首先添加user
		userDao.addUser(parms);
		//List<User> list = userDao.getAllusers(parms);
		String userId = parms.optString("ID");
		String roleId = parms.optString("ROLEID");
		String XM = parms.optString("XM");
		userDao.addUserRole(userId, roleId, XM);
		bool = true;
		return bool;
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.bringway.p2p.service.P2PUserService#deleteUser(net.sf.json.JSONObject)
	 */
	@Override
	public boolean deleteUser(JSONObject parms) {
		int i = userDao.deleteUser(parms);
		int j = userDao.deleteUser_Menu(parms);
		int k = userDao.deleteUser_Role(parms);
		return i>0;
	}

	/*
	 * (non-Javadoc)
	 * @see com.bringway.p2p.service.P2PUserService#updateUser(net.sf.json.JSONObject)
	 */
	@Override
	public boolean updateUser(JSONObject parms) {
		return userDao.updateUser(parms)>0;
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see com.bringway.p2p.service.P2PUserService#resetPassword(net.sf.json.
	 * JSONObject)
	 */
	@Override
	public boolean resetPassword(JSONObject parms) {
		return userDao.resetPassword(parms)>0;
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.bringway.p2p.service.P2PUserService#login(java.lang.String, java.lang.String)
	 */
	@Override
	public Map<String,Object> login(JSONObject params) {
		Map<String,Object> loginInfos = new HashMap<String,Object>();
		String msg = null;
		boolean isLogin = false;
		String gyh = params.optString("username");
		int i = userDao.getUserByGyh(gyh);
		if(i>0){
			//continue
			int j = userDao.getUserByGyhAndPassword(params);
			if(j>0){
				msg ="登陆成功！";
				isLogin = true;
			}else{
				msg = "密码错误！";
				isLogin = false;
			}
		}else{
			msg =  "用户名输入错误！";
			isLogin =  false;
		}
		loginInfos.put("msg", msg);
		loginInfos.put("isLogin", isLogin);
		return loginInfos;
	}
	
	
	/**
	 * @方法名称:addUserRole
	 * @方法描述:TODO
	 * @author qjy
	 * @date 2015年12月31日下午3:17:05
	 * @param userId
	 * @param roleId
	 * @param XM
	 */
	@Override
	public void addUserRole(String userId, String roleId,String XM) {
		userDao.addUserRole(userId,roleId,XM);
	}

	@Override
	public SysUserDetails login(String gyh) {
		return userDao.login(gyh);
	}

	@Override
	public int getPageCount(String tableName) {
		// TODO Auto-generated method stub
		return userDao.getPageCount(tableName);
	}

	@Override
	public boolean comparePassword(JSONObject parms) {
		return userDao.comparePassword(parms)>0;
	}

	@Override
	public boolean changePassword(JSONObject parms) {
		return userDao.changePassword(parms)>0;
	}

	@Override
	public int getUsersItems(JSONObject parms) {
		return userDao.getUsersItems(parms);
	}

	@Override
	public boolean updateAppUser(JSONObject parms) {
			return userDao.updateAppUser(parms)>0;
	
	}


	
}
